package com.yunshang.shoppingmall.entity;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.UUID;

/*图片合成类*/
public class MergeImage {


        public static void mergeImage(String bigPath, String smallPath, String x, String y) throws IOException {

            try {
                BufferedImage small;
                BufferedImage big = ImageIO.read(new File(bigPath));
                if (smallPath.contains("http")) {

                    URL url = new URL(smallPath);
                    small = ImageIO.read(url);
                } else {
                    small = ImageIO.read(new File(smallPath));
                }

                Graphics2D g = big.createGraphics();
                float fx = Float.parseFloat(x);
                float fy = Float.parseFloat(y);
                int x_i = (int) fx;
                int y_i = (int) fy;
                g.drawImage(small, x_i, y_i, small.getWidth(), small.getHeight(), null);

                g.dispose();
                ImageIO.write(big, "png", new File(bigPath));

            } catch (Exception e) {
                e.printStackTrace();
            }

        }

    public static String addWaterMark(String srcImgPath, String tarImgPath, String waterMarkContent, Color markContentColor, Font font, int x, int y) {
        try {
            // 读取原图片信息
            File srcImgFile = new File(srcImgPath);//得到文件
            Image srcImg = ImageIO.read(srcImgFile);//文件转化为图片
            int srcImgWidth = srcImg.getWidth(null);//获取图片的宽
            int srcImgHeight = srcImg.getHeight(null);//获取图片的高


            // 加水印
            BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = bufImg.createGraphics();
            g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
            g.setColor(markContentColor); //根据图片的背景设置水印颜色
            g.setFont(font); //设置字体

            //设置水印的坐标
//            int x = srcImgWidth - 2 * getWatermarkLength(waterMarkContent, g);
//            int y = srcImgHeight - 2 * getWatermarkLength(waterMarkContent, g);
            g.drawString(waterMarkContent, x, y);  //画出水印
            g.dispose();
            String uuId = UUID.randomUUID().toString().replace("-", "").substring(0, 15);
            String filename = "erwm"+uuId + ".png";
            //String localImgPath = "E:/images/uploads/ewmimages/";//本地
            String localImgPath="/usr/local/mall_uploads/";
            File file = new File(localImgPath);
            if (!file.exists()) {
                file.mkdirs();
            }

            tarImgPath = localImgPath + filename;
            // 输出图片
            FileOutputStream outImgStream = new FileOutputStream(tarImgPath);
            ImageIO.write(bufImg, "png", new File(tarImgPath));
            System.out.println("添加水印完成");
            outImgStream.flush();
            outImgStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tarImgPath;
    }


}
